<?php
namespace BaseBundle\Model;

use RedUnicorn\SymfonyKernel\Model\Model;
use Symfony\Component\DependencyInjection\ContainerInterface;
use RedUnicorn\Validate\Validate;
use BaseBundle\Entity\Article;

class m_article extends Model 
{

    /**
     * @var Article
     */
    private $article_class = null;

    function __construct(ContainerInterface $container)
    {   

        parent::__construct($container, 'article');
    }
 
    public function validate($data, $id = false)
    {
        $this->entityManager = $this->getDoctrine()->getManager();
        
        if(empty($id)){
            $this->article_class = new Article();
        }else{
            if(!$this->is_exist($id)){
                self::$error->setError($this->get('translator')->trans('未找到对应信息'));
                return false;
            }

            $this->article_class = $this->entityManager->getRepository(Article::class)->find($id);
        }
                        
        if(array_key_exists('admin_id', $data)){
            if($data['admin_id'] !== null) {
                $this->article_class->setAdmin($this->entityManager->getRepository(\AdminBundle\Entity\Admin::class)->find($data['admin_id']));
                if(empty($this->article_class->getAdmin())){
                    self::$error->setError($this->get('translator')->trans('admin_id:' . $data['admin_id'] . ' 未找到'));
                    return false;
                }
            }else{
                $this->article_class->setAdmin(null);
            }
        }
                         
        if(array_key_exists('article_class_id', $data)){
            if($data['article_class_id'] !== null) {
                $this->article_class->setArticleClass($this->entityManager->getRepository(\BaseBundle\Entity\ArticleClass::class)->find($data['article_class_id']));
                if(empty($this->article_class->getArticleClass())){
                    self::$error->setError($this->get('translator')->trans('article_class_id:' . $data['article_class_id'] . ' 未找到'));
                    return false;
                }
            }else{
                $this->article_class->setArticleClass(null);
            }
        }
                         
        if(array_key_exists('store_id', $data)){
            if($data['store_id'] !== null) {
                $this->article_class->setStore($this->entityManager->getRepository(\BaseBundle\Entity\Store::class)->find($data['store_id']));
                if(empty($this->article_class->getStore())){
                    self::$error->setError($this->get('translator')->trans('store_id:' . $data['store_id'] . ' 未找到'));
                    return false;
                }
            }else{
                $this->article_class->setStore(null);
            }
        }
                 
        if(array_key_exists('title', $data)){
            $this->article_class->setTitle($data['title']);
        }
                
        if(array_key_exists('content', $data)){
            $this->article_class->setContent($data['content']);
        }
                
        if(array_key_exists('is_del', $data)){
            $this->article_class->setIsDel($data['is_del']);
        }
                
        if(array_key_exists('is_recommend', $data)){
            $this->article_class->setIsRecommend($data['is_recommend']);
        }
                
        if(array_key_exists('show_image', $data)){
            $this->article_class->setShowImage($data['show_image']);
        }
                
        if(array_key_exists('view_times', $data)){
            $this->article_class->setViewTimes($data['view_times']);
        }
                
        if(array_key_exists('evaluate_times', $data)){
            $this->article_class->setEvaluateTimes($data['evaluate_times']);
        }
                
        if(array_key_exists('created_at', $data)){
            $this->article_class->setCreatedAt($data['created_at']);
        }
        
        if(self::$error->validate($this->article_class)){
            return false;
        }
        
        return true;
    }
    
    public function insert($data)
    {
        if(!$this->validate($data)){
            return false;
        }
        
        $this->article_class->setCreatedAt(new \DateTime());

        $this->entityManager->persist($this->article_class);
        $this->entityManager->flush();
    
        $this->lastInsertId = $this->article_class->getId();

        return true;
    }
    
    public function update($id ,$data)
    {
        if(empty($id)){
            self::$error->setError('param id is not null');
            return false;
        }
        
        if(!$this->validate($data, $id)){
            return false;
        }
                                
        $this->article_class->setUpdateAt(new \DateTime()); 
   
        $this->entityManager->flush();

        return true;
    }
    
    public function rewritesRule(&$rule)
    {   
                    
        //where   
        if(array_key_exists('id', $rule) && !empty($rule['id'])){
           $this->sql_array['where'] .= " AND sql_pre.id = '{$rule['id']}' ";
        }
                    
        //where   
        if(array_key_exists('admin_id', $rule) && !empty($rule['admin_id'])){
           $this->sql_array['where'] .= " AND sql_pre.admin_id = '{$rule['admin_id']}' ";
        }            
        //where   
        if(array_key_exists('article_class_id', $rule) && !empty($rule['article_class_id']) && $rule['article_class_id'] != -1){
           $this->sql_array['where'] .= " AND sql_pre.article_class_id = '{$rule['article_class_id']}' ";
        }            
        //where   
        if(array_key_exists('store_id', $rule) && !empty($rule['store_id'])){
           $this->sql_array['where'] .= " AND sql_pre.store_id = '{$rule['store_id']}' ";
        }            
        //where   
        if(array_key_exists('title', $rule) && !Validate::isRealEmpty($rule['title'])){
            $this->sql_array['where'] .= " AND sql_pre.title LIKE '{$rule['title']}' ";
        }

        if(array_key_exists('title_like', $rule) && !Validate::isRealEmpty($rule['title_like'])){
            $this->sql_array['where'] .= " AND sql_pre.title LIKE '%{$rule['title_like']}%' ";
        }
                    
        //where   
        if(array_key_exists('content', $rule) && !Validate::isRealEmpty($rule['content'])){
            $this->sql_array['where'] .= " AND sql_pre.content LIKE '{$rule['content']}' ";
        }
                    
        //where   
        if(array_key_exists('is_del', $rule)){
           $this->sql_array['where'] .= " AND sql_pre.is_del = '{$rule['is_del']}' ";
        }else{
            $this->sql_array['where'] .= " AND sql_pre.is_del = '0' ";
        }
                    
        //where
        if(array_key_exists('is_recommend', $rule) && $rule['is_recommend'] !== '' && $rule['is_recommend'] != '-1'){
            $this->sql_array['where'] .= " AND sql_pre.is_recommend = '{$rule['is_recommend']}' ";
        }
                    
        //where   
        if(array_key_exists('show_image', $rule) && !Validate::isRealEmpty($rule['show_image'])){
            $this->sql_array['where'] .= " AND sql_pre.show_image LIKE '{$rule['show_image']}' ";
        }
                    
        //where   
        if(array_key_exists('view_times', $rule)){
           $this->sql_array['where'] .= " AND sql_pre.view_times = '{$rule['view_times']}' ";
        }
                    
        //where   
        if(array_key_exists('evaluate_times', $rule)){
           $this->sql_array['where'] .= " AND sql_pre.evaluate_times = '{$rule['evaluate_times']}' ";
        }
                    
        //where   
        if(array_key_exists('created_at', $rule) && !Validate::isRealEmpty($rule['created_at'])){
            $this->sql_array['where'] .= " AND sql_pre.created_at LIKE '{$rule['created_at']}' ";
        }

        if(array_key_exists('created_at', $rule) && !Validate::isRealEmpty($rule['created_at'])){
            $this->sql_array['where'] .= " AND sql_pre.created_at LIKE '{$rule['created_at']}' ";
        }

        if(array_key_exists('created_at', $rule) && !Validate::isRealEmpty($rule['created_at'])){
            $this->sql_array['where'] .= " AND sql_pre.created_at LIKE '{$rule['created_at']}' ";
        }
                    
        //where   
        if(array_key_exists('update_at', $rule) && !Validate::isRealEmpty($rule['update_at'])){
            $this->sql_array['where'] .= " AND sql_pre.update_at LIKE '{$rule['update_at']}' ";
        }

        //where
        if(array_key_exists('where_key_word', $rule) && !Validate::isRealEmpty($rule['where_key_word'])){
            $this->sql_array['where'] .= " AND (sql_pre.title LIKE '%{$rule['where_key_word']}%' or ac.article_class_name like '%{$rule['where_key_word']}%') ";
        }

        m_article_class::joinInfo($this,$rule);
            
    }

    public function rewriteInfo(&$info)
    {
        parent::rewriteInfo($info);

        m_article_class::setJoinInfo($this,$info);

        if (array_key_exists('is_recommend',$info)){
            $info['is_recommend_str'] = $this->get('unicorn.array')->value($this->getParameter('is_true_arr'), $info['is_recommend']);
        }

        if (array_key_exists('show_image',$info)){
            $info['show_image_path'] = $this->get('unicorn.files')->getFilePath($info['show_image']);
        }

        if (array_key_exists('content',$info)){
            $pre_url = $this->getParameter('image_pre');
            $info['content'] = str_replace('<img src="', '<img src="'.$pre_url,$info['content']);
        }

        return $info;
    }




}